#include <iostream>

#include "data.h"

using namespace std;

pthread_mutex_t mutex;
float weight;
float thresh;

float max_weight = 1;
float max_thresh = 1.5;

bool end;

float calculate_result(int *numbers, int count)
{
   float result = 0;
   for(int cntr = 0; cntr < count; cntr ++)
      result += weight * numbers[cntr];
   return result;
}

void iterate_weight(neuron_data_t *data)
{
   int factor = 1;

   for(int cntr1 = 0; cntr1 < data->rows; cntr1++)
   {
      cout<<"Iterating before equation "<<cntr1<<endl;

      bool state = false;
      int out = data->inputs[cntr1][data->columns - 1];

      while(state == false)
      {
         cout<<"\tw = "<<weight<<" q = "<<thresh<<endl;	
         float result = 0;
         for(int cntr2 = 0; cntr2 < (data->columns - 1); cntr2++)
            result += data->inputs[cntr1][cntr2] * weight;
         if(result > thresh && out == 1)
            state = true;
         else if(result < thresh && out == 0)
            state = true;
         else if(result >= thresh && out == 0)
         {
            while(result >= thresh)
            {
               if(factor == -1)
               {
                  weight -= 0.1;
               }
               else
               {
                  thresh += 0.1;
                  if(thresh > max_thresh)
                  {
                     factor = -1;
                     thresh = 0;
                     break;
                  }
               }

               result = calculate_result(data->inputs[cntr1], (data->columns - 1));
            }
            cntr1 = -1;
            break;
         }
         else if(result <= thresh && out == 1)
         {
            while(result <= thresh)
            {
               if(factor == -1)
                  thresh -= 0.1;
               else
               {
                  weight += 0.1;
                  if(weight > max_weight)
                  {
                     factor = -1;
                     weight = 0;
                     break;
                  }
               }

               result = calculate_result(data->inputs[cntr1], (data->columns - 1));
            }
            cntr1 = -1;
            break;
         }
      }
   }
}

int main()
{
   weight = 0;
   thresh = 0;
   end = false;

   neuron_data_t input;
   cout<<"Training the neuron to a specific function"<<endl;
   cout<<"Please enter the number of rows in sample: ";
   cin>>input.rows;
   cout<<"Please enter the number of columns in sample: ";
   cin>>input.columns;
   int rows = input.rows;
   int columns = input.columns;

   input.inputs = new int* [rows];
   for(int cntr = 0; cntr < rows; cntr++)
      input.inputs[cntr] = new int [columns];

   for(int cntr1 = 0; cntr1 < rows; cntr1++)
   {
      cout<<"Enter sample in "<<(cntr1 + 1)<<endl;
      for(int cntr2 = 0; cntr2 < columns; cntr2++)
      {
         cout<<"\tEnter "<<(cntr2 + 1)<<"th value: ";
         cin>>input.inputs[cntr1][cntr2];
      }
   } 

   iterate_weight(&input);

   cout<<"\n\nAlgorithm learned now you may test"<<endl;

   while(1)
   {
      int num_inputs;
      int *new_inputs;
      cout<<"Enter number of inputs: ";
      cin>>num_inputs;
      float new_thresh = (thresh / (input.columns - 1)) * num_inputs;
      new_inputs = new int [num_inputs];
      for(int cntr = 0; cntr < num_inputs; cntr++)
      {
         cout<<"Enter number"<<(cntr + 1)<<": ";
         cin>>new_inputs[cntr];
      }

      float result = calculate_result(new_inputs, num_inputs);
      if(result > new_thresh)
         cout<<"Output: 1"<<endl;
      else
         cout<<"Output: 0"<<endl;
      cout<<"Enter 1 to exit or else continue: ";
      cin>>num_inputs;
      if(num_inputs == 1)break;
   }

   return 0;
}
